package com.castudio.Service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castudio.Common.RespPageEntity;
import com.castudio.Entity.Dto.UpdateR;
import com.castudio.Entity.Repairtable;
import com.castudio.Entity.User;
import com.castudio.Mapper.RepairtableMapper;
import com.castudio.Service.RepairtableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castudio.Util.ApiResult;
import com.castudio.Util.TimeUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 小陈同学
 * @since 2022-12-14
 */
@Service
public class RepairtableServiceImpl extends ServiceImpl<RepairtableMapper, Repairtable> implements RepairtableService {

    @Autowired
    private RepairtableMapper reairtableMapper;

    @Override
    public RespPageEntity getAllPage(Integer page, Integer size) {

        RespPageEntity pageEntity = new RespPageEntity();
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        // 获取当前也用户信息
        List<Repairtable> repair = reairtableMapper.getAllPage(page, size);
        pageEntity.setData(repair);
        // 获取当前用户总量
        Long total = reairtableMapper.getTotal();
        pageEntity.setTotal(total);
        return pageEntity;
    }

    @Override
    public RespPageEntity page(Integer page, Integer size, String openid) {
        RespPageEntity pageEntity = new RespPageEntity();
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        // 获取当前也用户信息
        List<Repairtable> repair = reairtableMapper.Page(page, size, openid);
        pageEntity.setData(repair);
        // 获取当前用户总量
        Long total = reairtableMapper.Total(openid);
        pageEntity.setTotal(total);
        return pageEntity;
    }

    @Override
    public void save(String fileUrl, String computername, String computertype, String reason, String realname,
                     String stunum, String dormnum, String phone, String time, String openid) {
        reairtableMapper.insert(fileUrl, computertype, computername, reason, realname, dormnum, stunum, phone, time, openid);
    }

    @Override
    public String updateR(UpdateR r) {
        System.out.println("订单id:" + r.getId());
        Integer id = r.getId();
        if (id == null) {
            return ApiResult.args();
        }
        String repairid = r.getRepairid();
        String time = TimeUtils.getNowDateTime();
        reairtableMapper.updateR(id, repairid, time);
        return ApiResult.ok(r);
    }

    @Override
    public String updateS(UpdateR r) {
        System.out.println("订单id:" + r.getId());
        Integer id = r.getId();
        if (id == null) {
            return ApiResult.args();
        }
        Integer state = 0;
        String time = TimeUtils.getNowDateTime();
        String remark = r.getRemark();
        if (remark == null) {
            return ApiResult.error("请填写备注，不然无法完成上传");
        }
        reairtableMapper.updateS(id, state, time, remark);
        return ApiResult.ok(r);
    }

    @Override
    public IPage<Repairtable> getPage(Integer currentPage, Integer pageSize, Repairtable repairtable) {
        LambdaQueryWrapper<Repairtable> queryWrapper = new LambdaQueryWrapper<Repairtable>();
        queryWrapper.like(Strings.isNotEmpty(repairtable.getRealname()), Repairtable::getRealname, repairtable.getRealname());
//        queryWrapper.select().orderByAsc(Repairtable::getCreatetime);
        IPage page = new Page(currentPage, pageSize);
        reairtableMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public IPage<Repairtable> getPage(Integer currentPage, Integer pageSize) {
        IPage page = new Page(currentPage, pageSize);
        reairtableMapper.selectPage(page, null);
        return page;
    }


}
